// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.chat;

import java.util.ArrayList;
import java.util.HashMap;
import zombie.characters.IsoPlayer;
import zombie.core.Color;
import zombie.core.Colors;
import zombie.core.raknet.UdpConnection;
import zombie.core.textures.Texture;
import zombie.debug.DebugLog;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.ServerOptions;
import zombie.network.chat.ChatType;

public final class ChatUtility {
    private static final boolean useEuclidean = true;
    private static final HashMap<String, String> allowedChatIcons = new HashMap();
    private static final HashMap<String, String> allowedChatIconsFull = new HashMap();
    private static final StringBuilder builder = new StringBuilder();
    private static final StringBuilder builderTest = new StringBuilder();

    private ChatUtility() {
    }

    public static float getScrambleValue(IsoObject src, IsoPlayer dest, float baseRange) {
        return getScrambleValue(src.getX(), src.getY(), src.getZ(), src.getSquare(), dest, baseRange);
    }

    public static float getScrambleValue(float srcX, float srcY, float srcZ, IsoGridSquare srcSquare, IsoPlayer dest, float baseRange) {
        float float0 = 1.0F;
        boolean boolean0 = false;
        boolean boolean1 = false;
        if (srcSquare != null && dest.getSquare() != null) {
            if (dest.getBuilding() != null && srcSquare.getBuilding() != null && dest.getBuilding() == srcSquare.getBuilding()) {
                if (dest.getSquare().getRoom() == srcSquare.getRoom()) {
                    float0 = (float)((double)float0 * 2.0);
                    boolean1 = true;
                } else if (Math.abs(dest.getZ() - srcZ) < 1.0F) {
                    float0 = (float)((double)float0 * 2.0);
                }
            } else if (dest.getBuilding() != null || srcSquare.getBuilding() != null) {
                float0 = (float)((double)float0 * 0.5);
                boolean0 = true;
            }

            if (Math.abs(dest.getZ() - srcZ) >= 1.0F) {
                float0 = (float)((double)float0 - (double)float0 * (double)Math.abs(dest.getZ() - srcZ) * 0.25);
                boolean0 = true;
            }
        }

        float float1 = baseRange * float0;
        float float2 = 1.0F;
        if (float0 > 0.0F && playerWithinBounds(srcX, srcY, dest, float1)) {
            float float3 = getDistance(srcX, srcY, dest);
            if (float3 >= 0.0F && float3 < float1) {
                float float4 = float1 * 0.6F;
                if (!boolean1 && (boolean0 || !(float3 < float4))) {
                    if (float1 - float4 != 0.0F) {
                        float2 = (float3 - float4) / (float1 - float4);
                        if (float2 < 0.2F) {
                            float2 = 0.2F;
                        }
                    }
                } else {
                    float2 = 0.0F;
                }
            }
        }

        return float2;
    }

    public static boolean playerWithinBounds(IsoObject source, IsoObject dest, float dist) {
        return playerWithinBounds(source.getX(), source.getY(), dest, dist);
    }

    public static boolean playerWithinBounds(float srcX, float srcY, IsoObject dest, float dist) {
        return dest == null ? false : dest.getX() > srcX - dist && dest.getX() < srcX + dist && dest.getY() > srcY - dist && dest.getY() < srcY + dist;
    }

    public static float getDistance(IsoObject source, IsoPlayer dest) {
        return dest == null ? -1.0F : (float)Math.sqrt(Math.pow((double)(source.getX() - dest.x), 2.0) + Math.pow((double)(source.getY() - dest.y), 2.0));
    }

    public static float getDistance(float srcX, float srcY, IsoPlayer dest) {
        return dest == null ? -1.0F : (float)Math.sqrt(Math.pow((double)(srcX - dest.x), 2.0) + Math.pow((double)(srcY - dest.y), 2.0));
    }

    public static UdpConnection findConnection(short playerOnlineID) {
        UdpConnection udpConnection0 = null;
        if (GameServer.udpEngine != null) {
            for (int int0 = 0; int0 < GameServer.udpEngine.connections.size(); int0++) {
                UdpConnection udpConnection1 = (UdpConnection)GameServer.udpEngine.connections.get(int0);

                for (int int1 = 0; int1 < udpConnection1.playerIDs.length; int1++) {
                    if (udpConnection1.playerIDs[int1] == playerOnlineID) {
                        udpConnection0 = udpConnection1;
                        break;
                    }
                }
            }
        }

        if (udpConnection0 == null) {
            DebugLog.log("Connection with PlayerID ='" + playerOnlineID + "' not found!");
        }

        return udpConnection0;
    }

    public static UdpConnection findConnection(String playerName) {
        UdpConnection udpConnection0 = null;
        if (GameServer.udpEngine != null) {
            for (int int0 = 0; int0 < GameServer.udpEngine.connections.size() && udpConnection0 == null; int0++) {
                UdpConnection udpConnection1 = (UdpConnection)GameServer.udpEngine.connections.get(int0);

                for (int int1 = 0; int1 < udpConnection1.players.length; int1++) {
                    if (udpConnection1.players[int1] != null && udpConnection1.players[int1].username.equalsIgnoreCase(playerName)) {
                        udpConnection0 = udpConnection1;
                        break;
                    }
                }
            }
        }

        if (udpConnection0 == null) {
            DebugLog.log("Player with nickname = '" + playerName + "' not found!");
        }

        return udpConnection0;
    }

    public static IsoPlayer findPlayer(int playerOnlineID) {
        IsoPlayer isoPlayer = null;
        if (GameServer.udpEngine != null) {
            for (int int0 = 0; int0 < GameServer.udpEngine.connections.size(); int0++) {
                UdpConnection udpConnection = (UdpConnection)GameServer.udpEngine.connections.get(int0);

                for (int int1 = 0; int1 < udpConnection.playerIDs.length; int1++) {
                    if (udpConnection.playerIDs[int1] == playerOnlineID) {
                        isoPlayer = udpConnection.players[int1];
                        break;
                    }
                }
            }
        }

        if (isoPlayer == null) {
            DebugLog.log("Player with PlayerID ='" + playerOnlineID + "' not found!");
        }

        return isoPlayer;
    }

    public static String findPlayerName(int playerOnlineID) {
        return findPlayer(playerOnlineID).getUsername();
    }

    public static IsoPlayer findPlayer(String playerNickname) {
        IsoPlayer isoPlayer = null;
        if (GameClient.bClient) {
            isoPlayer = GameClient.instance.getPlayerFromUsername(playerNickname);
        } else if (GameServer.bServer) {
            isoPlayer = GameServer.getPlayerByUserName(playerNickname);
        }

        if (isoPlayer == null) {
            DebugLog.log("Player with nickname = '" + playerNickname + "' not found!");
        }

        return isoPlayer;
    }

    public static ArrayList<ChatType> getAllowedChatStreams() {
        String string0 = ServerOptions.getInstance().ChatStreams.getValue();
        string0 = string0.replaceAll("\"", "");
        String[] string1 = string0.split(",");
        ArrayList arrayList = new ArrayList();
        arrayList.add(ChatType.server);

        for (String string2 : string1) {
            switch (string2) {
                case "s":
                    arrayList.add(ChatType.say);
                    break;
                case "r":
                    arrayList.add(ChatType.radio);
                    break;
                case "a":
                    arrayList.add(ChatType.admin);
                    break;
                case "w":
                    arrayList.add(ChatType.whisper);
                    break;
                case "y":
                    arrayList.add(ChatType.shout);
                    break;
                case "sh":
                    arrayList.add(ChatType.safehouse);
                    break;
                case "f":
                    arrayList.add(ChatType.faction);
                    break;
                case "all":
                    arrayList.add(ChatType.general);
            }
        }

        return arrayList;
    }

    public static boolean chatStreamEnabled(ChatType type) {
        ArrayList arrayList = getAllowedChatStreams();
        return arrayList.contains(type);
    }

    public static void InitAllowedChatIcons() {
        allowedChatIcons.clear();
        Texture.collectAllIcons(allowedChatIcons, allowedChatIconsFull);
    }

    private static String getColorString(String string0, boolean _boolean) {
        if (Colors.ColorExists(string0)) {
            Color color = Colors.GetColorByName(string0);
            return _boolean
                ? color.getRedFloat() + "," + color.getGreenFloat() + "," + color.getBlueFloat()
                : color.getRed() + "," + color.getGreen() + "," + color.getBlue();
        } else {
            if (string0.length() <= 11 && string0.contains(",")) {
                String[] string1 = string0.split(",");
                if (string1.length == 3) {
                    int int0 = parseColorInt(string1[0]);
                    int int1 = parseColorInt(string1[1]);
                    int int2 = parseColorInt(string1[2]);
                    if (int0 != -1 && int1 != -1 && int2 != -1) {
                        if (_boolean) {
                            return (float)int0 / 255.0F + "," + (float)int1 / 255.0F + "," + (float)int2 / 255.0F;
                        }

                        return int0 + "," + int1 + "," + int2;
                    }
                }
            }

            return null;
        }
    }

    private static int parseColorInt(String string) {
        try {
            int _int = Integer.parseInt(string);
            return _int >= 0 && _int <= 255 ? _int : -1;
        } catch (Exception exception) {
            return -1;
        }
    }

    public static String parseStringForChatBubble(String str) {
        try {
            builder.delete(0, builder.length());
            builderTest.delete(0, builderTest.length());
            str = str.replaceAll("\\[br/]", "");
            str = str.replaceAll("\\[cdt=", "");
            char[] char0 = str.toCharArray();
            boolean boolean0 = false;
            boolean boolean1 = false;
            int int0 = 0;

            for (int int1 = 0; int1 < char0.length; int1++) {
                char char1 = char0[int1];
                if (char1 == '*') {
                    if (!boolean0) {
                        boolean0 = true;
                    } else {
                        String string0 = builderTest.toString();
                        builderTest.delete(0, builderTest.length());
                        String string1 = getColorString(string0, false);
                        if (string1 != null) {
                            if (boolean1) {
                                builder.append("[/]");
                            }

                            builder.append("[col=");
                            builder.append(string1);
                            builder.append(']');
                            boolean0 = false;
                            boolean1 = true;
                        } else if (int0 < 10 && (string0.equalsIgnoreCase("music") || allowedChatIcons.containsKey(string0.toLowerCase()))) {
                            if (boolean1) {
                                builder.append("[/]");
                                boolean1 = false;
                            }

                            builder.append("[img=");
                            builder.append(string0.equalsIgnoreCase("music") ? "music" : (String)allowedChatIcons.get(string0.toLowerCase()));
                            builder.append(']');
                            boolean0 = false;
                            int0++;
                        } else {
                            builder.append('*');
                            builder.append(string0);
                        }
                    }
                } else if (boolean0) {
                    builderTest.append(char1);
                } else {
                    builder.append(char1);
                }
            }

            if (boolean0) {
                builder.append('*');
                String string2 = builderTest.toString();
                if (string2.length() > 0) {
                    builder.append(string2);
                }

                if (boolean1) {
                    builder.append("[/]");
                }
            }

            return builder.toString();
        } catch (Exception exception) {
            exception.printStackTrace();
            return str;
        }
    }

    public static String parseStringForChatLog(String str) {
        try {
            builder.delete(0, builder.length());
            builderTest.delete(0, builderTest.length());
            char[] char0 = str.toCharArray();
            boolean boolean0 = false;
            boolean boolean1 = false;
            int int0 = 0;

            for (int int1 = 0; int1 < char0.length; int1++) {
                char char1 = char0[int1];
                if (char1 == '*') {
                    if (!boolean0) {
                        boolean0 = true;
                    } else {
                        String string0 = builderTest.toString();
                        builderTest.delete(0, builderTest.length());
                        String string1 = getColorString(string0, true);
                        if (string1 != null) {
                            builder.append(" <RGB:");
                            builder.append(string1);
                            builder.append('>');
                            boolean0 = false;
                            boolean1 = true;
                        } else {
                            if (int0 < 10 && (string0.equalsIgnoreCase("music") || allowedChatIconsFull.containsKey(string0.toLowerCase()))) {
                                if (boolean1) {
                                    builder.append(" <RGB:");
                                    builder.append("1.0,1.0,1.0");
                                    builder.append('>');
                                    boolean1 = false;
                                }

                                String string2 = string0.equalsIgnoreCase("music")
                                    ? "Icon_music_notes"
                                    : (String)allowedChatIconsFull.get(string0.toLowerCase());
                                Texture texture = Texture.getSharedTexture(string2);
                                if (Texture.getSharedTexture(string2) != null) {
                                    int int2 = (int)((float)texture.getWidth() * 0.5F);
                                    int int3 = (int)((float)texture.getHeight() * 0.5F);
                                    if (string0.equalsIgnoreCase("music")) {
                                        int2 = (int)((float)texture.getWidth() * 0.75F);
                                        int3 = (int)((float)texture.getHeight() * 0.75F);
                                    }

                                    builder.append("<IMAGE:");
                                    builder.append(string2);
                                    builder.append("," + int2 + "," + int3 + ">");
                                    boolean0 = false;
                                    int0++;
                                    continue;
                                }
                            }

                            builder.append('*');
                            builder.append(string0);
                        }
                    }
                } else if (boolean0) {
                    builderTest.append(char1);
                } else {
                    builder.append(char1);
                }
            }

            if (boolean0) {
                builder.append('*');
                String string3 = builderTest.toString();
                if (string3.length() > 0) {
                    builder.append(string3);
                }
            }

            return builder.toString();
        } catch (Exception exception) {
            exception.printStackTrace();
            return str;
        }
    }
}
